/**
 * 管理员修改密码
 */
package controller;

import hibernate.Adminstab;
import hibernate.AdminstabDAO;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class UpdatePwd implements Action{
	private String oldPSW;
	private String psw;
	private Adminstab adminstab;
	


	public String getOldPSW() {
		return oldPSW;
	}



	public void setOldPSW(String oldPSW) {
		this.oldPSW = oldPSW;
	}



	public String getPsw() {
		return psw;
	}



	public void setPsw(String psw) {
		this.psw = psw;
	}



	public String execute() throws Exception {
		AdminstabDAO adminstabDAO=new AdminstabDAO();
		adminstabDAO.getSession().beginTransaction();
		ActionContext ctx = ActionContext.getContext();
		boolean match = false;

		try{		
			adminstab = (Adminstab)adminstabDAO.findByAccount(ctx.getSession().get("account")).get(0);
			System.out.println(adminstab.getPwd().equals(oldPSW));
			if(adminstab.getPwd().equals(oldPSW)){
				adminstab.setPwd(psw);
				adminstabDAO.getSession().update(adminstab);
				adminstabDAO.getSession().getTransaction().commit();
				match = true;
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			adminstabDAO.getSession().close();
		}
		if(match == true){
			return SUCCESS;
		}
		else
			return ERROR;
	}

}
